//
//  SISSUploadPictureBottomInfoShowViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/25.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSUploadPictureBottomInfoShowViewController.h"
#import "SISSTitleView.h"
#import "SISSComplainCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface SISSUploadPictureBottomInfoShowViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,MWPhotoBrowserDelegate>

@property (weak, nonatomic) SISSTitleView *titleView;
@property (weak, nonatomic) UICollectionView *collectionView;

@property (weak, nonatomic) SISSTitleView *infoTitleView;
@property (weak, nonatomic) UILabel *infoLabel;

@property (weak, nonatomic) UIButton *editButton;

///
@property (assign, nonatomic) CGFloat collectViewHeight;

@end

@implementation SISSUploadPictureBottomInfoShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    self.titleView.titleStr = titleStr;
}

- (void)setInfoTitleStr:(NSString *)infoTitleStr {
    _infoTitleStr = infoTitleStr;
    self.infoTitleView.titleStr = infoTitleStr;
}

- (void)setInfoStr:(NSString *)infoStr {
    _infoStr = infoStr;
    self.infoLabel.text = infoStr;
}

- (void)setImagesUrlArr:(NSArray *)imagesUrlArr {
    _imagesUrlArr = imagesUrlArr;
    if (imagesUrlArr.count * 75 + 10 > SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(132);
        }];
        self.collectViewHeight = 132;
    }
    
    if (imagesUrlArr.count * 75 + 10 > 2 * SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(193);
        }];
        self.collectViewHeight = 193;
    }
    [self.collectionView reloadData];
}

- (void)setImages:(NSArray *)images {
    _images = images;
    if (images.count * 75 + 10 > SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(132);
        }];
        self.collectViewHeight = 132;
    }
    
    if (images.count * 75 + 10 > 2 * SCREEN_WIDTH) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(193);
        }];
        self.collectViewHeight = 193;
    }
    
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.images && self.images.count > 0 ? self.images.count : self.imagesUrlArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSComplainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SISSComplainCollectionViewCellReuseID" forIndexPath:indexPath];
    
    if (self.images.count > 0) {
        cell.imageView.image = self.images[indexPath.row];
    } else {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imagesUrlArr[indexPath.item]]];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    UIResponder *responder = self;
    UIViewController *viewController;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: [UIViewController class]])
            viewController = (UIViewController *)responder;
    
    MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [photoBrowser setCurrentPhotoIndex:indexPath.item];
    //    [photoBrowser showNextPhotoAnimated:YES];
    //    [photoBrowser showPreviousPhotoAnimated:YES];
    SISSNavigationController *navigationController = [[SISSNavigationController alloc] initWithRootViewController:photoBrowser];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [viewController presentViewController:navigationController animated:YES completion:nil];
}


- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.images.count > 0 ? self.images.count : self.imagesUrlArr.count;
}

- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    MWPhoto *photo;
    // 创建图片模型
    photo = self.images.count > 0 ? [MWPhoto photoWithImage:self.images[index]] : [MWPhoto photoWithURL:[NSURL URLWithString:self.imagesUrlArr[index]]];
    return photo;
}

- (void)editButtonClickAction {
    
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTitleView *titleView = [[SISSTitleView alloc] init];
    titleView.titleStr = @"上传公证书";
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    //    CGFloat itemW = (SCREEN_WIDTH - 3 * 10) / 3;
    //    flowLayout.itemSize = CGSizeMake(itemW, itemW);
    flowLayout.itemSize = CGSizeMake(65, 65);
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 10, 1, 10);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerNib:[UINib nibWithNibName:@"SISSComplainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SISSComplainCollectionViewCellReuseID"];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    SISSTitleView *infoTitleView = [[SISSTitleView alloc] init];
    infoTitleView.titleStr = @"补充说明";
    [self.view addSubview:infoTitleView];
    self.infoTitleView = infoTitleView;
    
    UILabel *infoLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    infoLabel.text = self.infoStr;
    [self.view addSubview:infoLabel];
    self.infoLabel = infoLabel;
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editButton setBackgroundColor:MAIN_COLOR];
    [editButton addTarget:self action:@selector(editButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    editButton.layer.cornerRadius = 4;
    editButton.layer.masksToBounds = YES;
    [self.view addSubview:editButton];
    self.editButton = editButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf;
    self.collectViewHeight = 66;
    CGSize buttonSize = CGSizeMake(SCREEN_WIDTH * 0.8, 40);
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(weakSelf.collectViewHeight);
    }];
    
    [self.infoTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.collectionView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.infoTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.infoLabel.mas_bottom).offset(8 * globelMargin);
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.size.mas_equalTo(buttonSize);
    }];
}


@end
